package com.nest.WorkNest.act;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPopupWindow;
import com.nest.WorkNest.R;

import java.util.ArrayList;
import java.util.Calendar;

public class CreateAct extends BaseActivity implements OnClickListener {
    private EditText edtTxt_name, edtTxt_createenquiry_inputfield, edtTxt_createenquiry_time, edtTxt_createenquiry_numbers, edtTxt_createenquiry_deliveryWay;
    private LinearLayout ll_createenquiry_demo, ll_createnquiry_yes, ll_createnquiry_no,
            ll_createnquiry_faa, ll_createnquiry_faa1, ll_createnquiry_faa2, ll_createnquiry_faa3,
            ll_createnquiry_faa4, ll_createnquiry_faa5, ll_createnquiry_package,
            ll_createnquiry_package1, ll_createnquiry_package2, ll_createnquiry_package3;
    private TextView tv_createnquiry_yes, tv_createnquiry_no, tv_createnquiry_faa1,
            tv_createnquiry_faa2, tv_createnquiry_faa3, tv_createnquiry_faa5,
            tv_createnquiry_package1, tv_createnquiry_package2, tv_createnquiry_package3;
    private String type, yesorno = "", faa = "", packages = "";
    private Intent mIntent;
    private Context context = CreateAct.this;
    private int mYear, mMonth, mDay;
    private Calendar calendar;
    private DatePickerDialog datePickerDialog;
    private OptionsPopupWindow pwOptions;
    private ArrayList<String> options1Items = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_creat);
        initBar();
        initView();
        initPicker();
        calendar = Calendar.getInstance();
        mIntent = getIntent();
        manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        actionbar_side_name.setVisibility(View.VISIBLE);
        ll_actionbar_side_left.setVisibility(View.VISIBLE);
        ll_tv_actionbar_side_left.setVisibility(View.VISIBLE);
        ll_iv_actionbar_side_left.setVisibility(View.VISIBLE);
        tv_actionbar_side_left.setText("保存");
        if (!(mIntent.getExtras() == null)) {
            type = mIntent.getExtras().getString("type");
            if (type.equals("name") || type.equals("PL")) {
                if (type.equals("name")) {
                    actionbar_side_name.setText("定制产品名称");
                } else {
                    actionbar_side_name.setText("产品链接");
                }
                initName(type);
            } else if (type.equals("demo") || type.equals("FON")) {
                if (type.equals("demo")) {
                    tv_createnquiry_no.setVisibility(View.VISIBLE);
                    tv_createnquiry_yes.setVisibility(View.GONE);
                    actionbar_side_name.setText("产品是否有样衣");
                } else {
                    tv_createnquiry_no.setVisibility(View.GONE);
                    tv_createnquiry_yes.setVisibility(View.VISIBLE);
                    actionbar_side_name.setText("打样前是否选主面料");
                }
                initDemo(type);
            } else if (type.equals("FAA")) {
                actionbar_side_name.setText("面辅料");
                initFAA();
            } else if (type.equals("packages") || type.equals("CL")) {
                if (type.equals("s")) {
                    actionbar_side_name.setText("包装");
                } else {
                    actionbar_side_name.setText("商品吊牌");
                }
                initPackage(type);
            } else if (type.equals("AR") || type.equals("PD") || type.equals("orderstate") || type.equals("RE")) {
                if (type.equals("AR")) {
                    actionbar_side_name.setText("面辅料备注");
                } else if (type.equals("PD")) {
                    actionbar_side_name.setText("产品详细说明");
                } else if (type.equals("orderstate")) {
                    actionbar_side_name.setText("订单说明");
                } else {
                    actionbar_side_name.setText("订单详细要求");
                }
                initInput(type);
            } else if (type.equals("time") || type.equals("bigtime") || type.equals("DT")) {
                if (type.equals("time")) {
                    actionbar_side_name.setText("样衣交付时间");
                } else if (type.equals("bigtime")) {
                    actionbar_side_name.setText("预计大货下单时间");
                } else if (type.equals("DT")) {
                    actionbar_side_name.setText("要求交货日期");
                }
                initTime(type);
            } else if (type.equals("numbers")) {
                actionbar_side_name.setText("预计每次订单数量");
                initNumber();
            } else if (type.equals("DW")) {
                actionbar_side_name.setText("选择交付方式");
                initdeliveryWay();
            }

        }
    }

    public void initView() {
        edtTxt_name = (EditText) findViewById(R.id.edtTxt_inputname_inputfield);
        edtTxt_createenquiry_inputfield = (EditText) findViewById(
                R.id.edtTxt_createenquiry_inputfield);
        edtTxt_createenquiry_time = (EditText) findViewById(R.id.edtTxt_createenquiry_time);
        edtTxt_createenquiry_numbers = (EditText) findViewById(R.id.edtTxt_createenquiry_numbers);
        edtTxt_createenquiry_deliveryWay = (EditText) findViewById(R.id.edtTxt_createenquiry_deliveryWay);
        ll_createenquiry_demo = (LinearLayout) findViewById(R.id.ll_createnquiry_demoornot);
        ll_createnquiry_yes = (LinearLayout) findViewById(R.id.ll_createnquiry_yes);
        ll_createnquiry_no = (LinearLayout) findViewById(R.id.ll_createnquiry_no);
        tv_createnquiry_yes = (TextView) findViewById(R.id.tv_createnquiry_yes);
        tv_createnquiry_no = (TextView) findViewById(R.id.tv_createnquiry_no);
        ll_createnquiry_faa = (LinearLayout) findViewById(R.id.ll_createnquiry_faa);
        ll_createnquiry_faa1 = (LinearLayout) findViewById(R.id.ll_createnquiry_faa1);
        ll_createnquiry_faa2 = (LinearLayout) findViewById(R.id.ll_createnquiry_faa2);
        ll_createnquiry_faa3 = (LinearLayout) findViewById(R.id.ll_createnquiry_faa3);
        ll_createnquiry_faa5 = (LinearLayout) findViewById(R.id.ll_createnquiry_faa5);
        tv_createnquiry_faa1 = (TextView) findViewById(R.id.tv_createnquiry_faa1);
        tv_createnquiry_faa2 = (TextView) findViewById(R.id.tv_createnquiry_faa2);
        tv_createnquiry_faa3 = (TextView) findViewById(R.id.tv_createnquiry_faa3);
        tv_createnquiry_faa5 = (TextView) findViewById(R.id.tv_createnquiry_faa5);
        ll_createnquiry_package = (LinearLayout) findViewById(R.id.ll_createnquiry_package);
        ll_createnquiry_package1 = (LinearLayout) findViewById(R.id.ll_createnquiry_package1);
        ll_createnquiry_package2 = (LinearLayout) findViewById(R.id.ll_createnquiry_package2);
        ll_createnquiry_package3 = (LinearLayout) findViewById(R.id.ll_createnquiry_package3);
        tv_createnquiry_package1 = (TextView) findViewById(R.id.tv_createnquiry_package1);
        tv_createnquiry_package2 = (TextView) findViewById(R.id.tv_createnquiry_package2);
        tv_createnquiry_package3 = (TextView) findViewById(R.id.tv_createnquiry_package3);
        edtTxt_createenquiry_time.setOnClickListener(this);
        edtTxt_createenquiry_deliveryWay.setOnClickListener(this);
    }

    public void initName(final String type) {
        edtTxt_name.setVisibility(View.VISIBLE);
        ll_actionbar_side_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent();
                String name = edtTxt_name.getText().toString();
                if (type.equals("name")) {
                    intent.putExtra("name", name);
                } else if (type.equals("PL")) {
                    intent.putExtra("PL", name);
                }
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        String text = "";
        if (type.equals("name")) {
            text = mIntent.getExtras().getString("name");
        } else if (type.equals("PL")) {
            text = mIntent.getExtras().getString("PL");
        }
        edtTxt_name.setText(text);
    }

    public void initDemo(final String type) {
        ll_createenquiry_demo.setVisibility(View.VISIBLE);
        ll_actionbar_side_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent();
                intent.putExtra("yesorno", yesorno);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        ll_createnquiry_yes.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                tv_createnquiry_yes.setVisibility(View.VISIBLE);
                tv_createnquiry_no.setVisibility(View.GONE);
                yesorno = "是";
                Intent intent = new Intent();
                intent.putExtra("yesorno", yesorno);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        ll_createnquiry_no.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                tv_createnquiry_no.setVisibility(View.VISIBLE);
                tv_createnquiry_yes.setVisibility(View.GONE);
                yesorno = "否";
                Intent intent = new Intent();
                intent.putExtra("yesorno", yesorno);
                setResult(RESULT_OK, intent);
                finish();

            }
        });
        String text = null;
        if (type.equals("demo")) {
            text = mIntent.getStringExtra("demo");
        } else if (type.equals("FON")) {
            text = mIntent.getStringExtra("FON");
        }
        if (text != null) {
            if (text.equals("是")) {
                tv_createnquiry_yes.setVisibility(View.VISIBLE);
                tv_createnquiry_no.setVisibility(View.GONE);
            } else if (text.equals("否")) {
                tv_createnquiry_no.setVisibility(View.VISIBLE);
                tv_createnquiry_yes.setVisibility(View.GONE);
            }
            yesorno = text;
        }

    }

    public void initFAA() {
        ll_createnquiry_faa.setVisibility(View.VISIBLE);
        if (TextUtils.isEmpty(faa)) {
            tv_createnquiry_faa1.setVisibility(View.VISIBLE);
            tv_createnquiry_faa2.setVisibility(View.GONE);
            tv_createnquiry_faa3.setVisibility(View.GONE);
            tv_createnquiry_faa5.setVisibility(View.GONE);
        }
        ll_actionbar_side_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent();
                intent.putExtra("faa", faa);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        ll_createnquiry_faa1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                tv_createnquiry_faa1.setVisibility(View.VISIBLE);
                tv_createnquiry_faa2.setVisibility(View.GONE);
                tv_createnquiry_faa3.setVisibility(View.GONE);
                tv_createnquiry_faa5.setVisibility(View.GONE);
                faa = "包工包料";
                Intent intent = new Intent();
                intent.putExtra("faa", faa);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        ll_createnquiry_faa2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                tv_createnquiry_faa2.setVisibility(View.VISIBLE);
                tv_createnquiry_faa1.setVisibility(View.GONE);
                tv_createnquiry_faa3.setVisibility(View.GONE);
                tv_createnquiry_faa5.setVisibility(View.GONE);
                faa = "清加工";
                Intent intent = new Intent();
                intent.putExtra("faa", faa);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        ll_createnquiry_faa3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                tv_createnquiry_faa3.setVisibility(View.VISIBLE);
                tv_createnquiry_faa2.setVisibility(View.GONE);
                tv_createnquiry_faa1.setVisibility(View.GONE);
                tv_createnquiry_faa5.setVisibility(View.GONE);
                faa = "我指定面辅料供应商";
                Intent intent = new Intent();
                intent.putExtra("faa", faa);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        ll_createnquiry_faa5.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                tv_createnquiry_faa5.setVisibility(View.VISIBLE);
                tv_createnquiry_faa2.setVisibility(View.GONE);
                tv_createnquiry_faa3.setVisibility(View.GONE);
                tv_createnquiry_faa1.setVisibility(View.GONE);
                faa = "其他";
                Intent intent = new Intent();
                intent.putExtra("faa", faa);
                setResult(RESULT_OK, intent);
                finish();
            }
        });

        String text = mIntent.getExtras().getString("faa");
        if (text != null) {
            if (text.equals("包工包料")) {
                tv_createnquiry_faa1.setVisibility(View.VISIBLE);
                tv_createnquiry_faa2.setVisibility(View.GONE);
                tv_createnquiry_faa3.setVisibility(View.GONE);
                tv_createnquiry_faa5.setVisibility(View.GONE);
                faa = "包工包料";
            } else if (text.equals("我指定面辅料")) {
                tv_createnquiry_faa3.setVisibility(View.VISIBLE);
                tv_createnquiry_faa2.setVisibility(View.GONE);
                tv_createnquiry_faa1.setVisibility(View.GONE);
                tv_createnquiry_faa5.setVisibility(View.GONE);
                faa = "我指定面辅料";
            } else if (text.equals("其他")) {
                tv_createnquiry_faa5.setVisibility(View.VISIBLE);
                tv_createnquiry_faa2.setVisibility(View.GONE);
                tv_createnquiry_faa3.setVisibility(View.GONE);
                tv_createnquiry_faa1.setVisibility(View.GONE);
                faa = "其他";
            } else if (text.equals("清加工")) {
                tv_createnquiry_faa2.setVisibility(View.VISIBLE);
                tv_createnquiry_faa1.setVisibility(View.GONE);
                tv_createnquiry_faa3.setVisibility(View.GONE);
                tv_createnquiry_faa5.setVisibility(View.GONE);
                faa = "清加工";
            }
        }
    }

    public void initPackage(final String type) {
        ll_createnquiry_package.setVisibility(View.VISIBLE);
        if (TextUtils.isEmpty(packages)) {
            tv_createnquiry_package3.setVisibility(View.VISIBLE);
            tv_createnquiry_package2.setVisibility(View.GONE);
            tv_createnquiry_package1.setVisibility(View.GONE);
        }
        ll_actionbar_side_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent();
                intent.putExtra("packages", packages);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        ll_createnquiry_package1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                tv_createnquiry_package1.setVisibility(View.VISIBLE);
                tv_createnquiry_package2.setVisibility(View.GONE);
                tv_createnquiry_package3.setVisibility(View.GONE);
                packages = "我提供";
                Intent intent = new Intent();
                intent.putExtra("packages", packages);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        ll_createnquiry_package2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                tv_createnquiry_package2.setVisibility(View.VISIBLE);
                tv_createnquiry_package1.setVisibility(View.GONE);
                tv_createnquiry_package3.setVisibility(View.GONE);
                packages = "不需要";
                Intent intent = new Intent();
                intent.putExtra("packages", packages);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        ll_createnquiry_package3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                tv_createnquiry_package3.setVisibility(View.VISIBLE);
                tv_createnquiry_package2.setVisibility(View.GONE);
                tv_createnquiry_package1.setVisibility(View.GONE);
                packages = "委托采购";
                Intent intent = new Intent();
                intent.putExtra("packages", packages);
                setResult(RESULT_OK, intent);
                finish();
            }
        });

        String text = null;
        if (type.equals("packages")) {
            text = mIntent.getStringExtra("packages");
        } else if (type.equals("CL")) {
            text = mIntent.getStringExtra("CL");
        }
        if (text != null) {
            if (text.equals("我提供")) {
                tv_createnquiry_package1.setVisibility(View.VISIBLE);
                tv_createnquiry_package2.setVisibility(View.GONE);
                tv_createnquiry_package3.setVisibility(View.GONE);
            } else if (text.equals("不需要")) {
                tv_createnquiry_package2.setVisibility(View.VISIBLE);
                tv_createnquiry_package3.setVisibility(View.GONE);
                tv_createnquiry_package1.setVisibility(View.GONE);
            } else if (text.equals("委托采购")) {
                tv_createnquiry_package3.setVisibility(View.VISIBLE);
                tv_createnquiry_package2.setVisibility(View.GONE);
                tv_createnquiry_package1.setVisibility(View.GONE);
            }
            packages = text;
        }
    }

    public void initInput(final String type) {
        edtTxt_createenquiry_inputfield.setVisibility(View.VISIBLE);

        ll_actionbar_side_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent();
                String input = edtTxt_createenquiry_inputfield.getText().toString();

                if (type.equals("AR")) {
                    intent.putExtra("AR", input);
                } else if (type.equals("PD")) {
                    intent.putExtra("PD", input);
                } else if (type.equals("RE")) {
                    intent.putExtra("RE", input);
                }
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        String text = "";
        if (type.equals("AR")) {
            text = mIntent.getExtras().getString("AR");
        } else if (type.equals("PD")) {
            text = mIntent.getExtras().getString("PD");
        } else if (type.equals("RE")) {
            text = mIntent.getExtras().getString("RE");
        }
        edtTxt_createenquiry_inputfield.setText(text);
    }

    public void initTime(final String type) {
        edtTxt_createenquiry_time.setVisibility(View.VISIBLE);
        ll_actionbar_side_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent();
                String time = edtTxt_createenquiry_time.getText().toString();
                intent.putExtra("time", time);
                setResult(RESULT_OK, intent);
                finish();

            }
        });
        String text = "";
        if (type.equals("time")) {
            text = mIntent.getExtras().getString("time");
        } else if (type.equals("bigtime")) {
            text = mIntent.getExtras().getString("bigtime");
        } else if (type.equals("DT")) {
            text = mIntent.getExtras().getString("DT");
        }
        edtTxt_createenquiry_time.setText(text);
    }

    public void initNumber() {
        edtTxt_createenquiry_numbers.setVisibility(View.VISIBLE);
        ll_actionbar_side_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent();
                String time = edtTxt_createenquiry_numbers.getText().toString();
                intent.putExtra("numbers", time);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        String text = mIntent.getExtras().getString("numbers");
        edtTxt_createenquiry_numbers.setText(text);
    }

    public void initdeliveryWay() {
        edtTxt_createenquiry_deliveryWay.setVisibility(View.VISIBLE);
        ll_actionbar_side_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent();
                String time = edtTxt_createenquiry_deliveryWay.getText().toString();
                intent.putExtra("DW", time);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        String text = mIntent.getExtras().getString("DW");
        edtTxt_createenquiry_deliveryWay.setText(text);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.edtTxt_createenquiry_time:
                datePickerDialog = new DatePickerDialog(context, AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int day) {
                        mYear = year;
                        mMonth = month;
                        mDay = day;
                        //更新EditText控件日期 小于10加0
                        edtTxt_createenquiry_time.setText(new StringBuilder().append(mYear).append("-")
                                .append((mMonth + 1) < 10 ? 0 + (mMonth + 1) : (mMonth + 1))
                                .append("-")
                                .append((mDay < 10) ? 0 + mDay : mDay));
                    }
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                        calendar.get(Calendar.DAY_OF_MONTH));
                datePickerDialog.setTitle("选择日期");
                datePickerDialog.show();
                break;

            case R.id.edtTxt_createenquiry_deliveryWay:
                pwOptions.showAtLocation(v, Gravity.BOTTOM, 0, 0);
                break;
        }
    }

    public void initPicker() {
        pwOptions = new OptionsPopupWindow(this);
        options1Items.add("自提");
        options1Items.add("送货上门");
        options1Items.add("物流到付");
        options1Items.add("快递到付");
        pwOptions.setPicker(options1Items);
        pwOptions.setSelectOptions(0);
        pwOptions.setOnoptionsSelectListener(new OptionsPopupWindow.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3) {
                //返回的分别是三个级别的选中位置
                String tx = options1Items.get(options1);
                edtTxt_createenquiry_deliveryWay.setText(tx);
            }
        });
    }

    public boolean onTouchEvent(MotionEvent event) {
        try {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {
                    manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return super.onTouchEvent(event);
    }
}
